var goodsId = '';
var shopId = '';
var dataTemp = []; // 保存用户评价
var pushEvaluation = false; // 清空/追加评价数量
var footClick = true;  // 是否查看更多
var pageNo = 1;
var pageSize = 10;
var evalStatus = 0; // 评论的状态   0全部 1好评  2差评  3有图

$(document).ready(function(){  
  shopId = '0600400007120190215'; 
  goodsId = '12';
  console.log(baseUrl)
  console.log(pirUrl)
  getHotCategory();
  getShopDetail();
  getGoodsDetail();
  getEvaluationTotal();
  getEvaluation();
});

// 收藏商品
$('#goods-detail').on('click','.collection-shop',function(){
  collectionShop();
});

// 取消收藏商品
$('#goods-detail').on('click','.clear-collection-shop',function(){
  clearCollectionShop();
});

// 关注店铺
$('#shop-detail').on('click','.shop-focus',function(){
  shopFocus();
});

// 取消关注店铺
$('#shop-detail').on('click','.clear-shop-focus',function(){
  clearShopFocus();
});

// 切换评论类型
$('.evaluation-box').on('click','.com-category span',function(){
  if(!$(this).hasClass('span')){
    $(this).addClass('span').siblings().removeClass('span');
    $('.more').text('查看更多评价');
    $('.more').append('<i class="fa fa-caret-down"></i>');
    pushEvaluation = false;
    footClick = true;
    pageNo = 1;
    evalStatus = $(this).data('evaluation-state');
    getEvaluation();
  }
});

// 查看更多评价
$('.more').click(function(){
  pushEvaluation = true;
  pageNo += 1;
  getEvaluation();
});

// 收藏商品
function collectionShop(){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/collectGoodsRecord/addCollectGoods",
    type:"get",	
    data:"goodsId="+goodsId,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        getGoodsDetail();
      }
    },
    error:function(res){

    },
  })
};

// 取消收藏
function clearCollectionShop(){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/collectGoodsRecord/removeCollectGoods",
    type:"get",	
    data:"goodsId="+goodsId,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        getGoodsDetail();
      }
    },
    error:function(res){

    },
  })
};

// 关注店铺
function shopFocus(){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/collectShopRecord/addCollectShop",
    type:"get",	
    data:"shopId="+shopId,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        getShopDetail();
      }
    },
    error:function(res){

    },
  })
};

// 取消关注店铺
function clearShopFocus(){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/collectShopRecord/removeCollectShops",
    type:"get",	
    data:"shopId="+shopId,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        getShopDetail();
      }
    },
    error:function(res){

    },
  })
};

// 获取商品详情
function getGoodsDetail(){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/goodsBasicInfo/findGoodsInfoDetail",
    type:"get",	
    data:"goodsId="+goodsId,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        shopId = res.data.shopId;
        var data={
          data:res.data,
        }
        var html = template('goods-script', data);
        var html2 = template('goods-service-script', data);
        document.getElementById('goods-detail').innerHTML = html;
				document.getElementById('service-detail').innerHTML = html2;
      }
    },
    error:function(res){
          
    },
  })
};

// 获取店铺详情
function getShopDetail(){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/shop/findShopBasicInfo",
    type:"get",	
    data:"shopId="+shopId,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        var data={
          data:res.data,
        }
        var html = template('shop-script', data);
        document.getElementById('shop-detail').innerHTML = html;
      }
    },
    error:function(res){
          
    },
  })
};

// 获取用户评价总条数
function getEvaluationTotal(){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/evaluation/initEvaluationByGoodsId",
    type:"get",	
    data:"goodsId="+goodsId,	
    dataType:"json", 
    success:function(res){
      console.log(res);
      var tempData = res.data;
      tempData.round = tempData.shopEvaluateValue==null?0:Math.round(tempData.shopEvaluateValue);
      tempData.cha = 5 - tempData.round;
      console.log(tempData);
      if(res.status === 1){
        var data={
          data:tempData,
        }
        var html = template('evaluation-total', data);
        document.getElementById('evaluation-box').innerHTML = html;
      }
    },
    error:function(res){

    },
  })
};

// 获取用户评价内容
function getEvaluation(){
  if(footClick){
    $.ajax({
      url: baseUrl+"/hsent-swfw-user/evaluation/findEvaluationByGoodsId",
      type:"get",	
      data:"goodsId="+goodsId+
      '&pageNo='+pageNo+
      '&pageSize='+pageSize+
      '&evalStatus='+evalStatus,	
      dataType:"json", 
      success:function(res){
        console.log(res);
        if(res.status === 1){
          if(pushEvaluation){
            if(res.data.evalList.length){
              for(var j = 0 ; j < res.data.evalList.length ; j ++){
                dataTemp.push(res.data.evalList[j]);
              }           
            }else{
              $('.more').text('暂无更多评价');
              footClick = false;
            }
            console.log('我是追加')
          }else{
            dataTemp = res.data.evalList;
          };
          pushEvaluation = false;
          for(var i = 0 ; i < dataTemp.length ; i ++){
            dataTemp[i].evaluateValue = Math.round(dataTemp[i].evaluateValue);
            dataTemp[i].cha = 5 - dataTemp[i].evaluateValue;
          }
          console.log(dataTemp)
          var data={
            data:dataTemp,
            pirUrl:pirUrl
          }
          var html = template('evaluation-list-script', data);
          document.getElementById('evaluation-list').innerHTML = html;
        }
      },
      error:function(res){
  
      },
    })
  }
};
// 获取热门类目
function getHotCategory(){
  $.ajax({
    url: baseUrl+"/hsent-swfw-user/homeCategoryController/findHomeCategory",
    type:"get",	
    data:"",	
    dataType:"json", 
    success:function(res){
      console.log(res);
      if(res.status === 1){
        var data={
          data:res.data,
        }
        var html = template('top-nav-script', data);
        document.getElementById('top-nav').innerHTML = html;
      }
    },
    error:function(res){

    },
  })
};

// ============== 分割线 ================
//锚链接js
$('.mao a').each(function(index) {
  $('.mao').children(":first").css({'color':'red'});
  $(this).click(function() {
    $(this).css({
      'color': 'red'
    }).siblings().css({
      'color': 'rgb(16,16,16)'
    });

    if(index == 0) {

      $('.shopsdetai .infor .infor-top').css({
        'color': 'red'
      })
      $('.shopsdetai .comments .com-top h4').css({
        'color': 'rgb(16,16,16)'
      })
    } else {

      $('.shopsdetai .comments .com-top h4').css({
        'color': 'red'
      })
      $('.shopsdetai .infor .infor-top').css({
        'color': 'rgb(16,16,16)'
      })
    }

  })
});